package com.itkim.YZUserInfo.controller;

import java.io.IOException;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.itkim.YZUserInfo.domain.YzUser;
import com.itkim.YZUserInfo.service.IYzUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 账号管理Controller
 * 
 * @author itkim
 * @date 2024-06-04
 */
@Controller
@RequestMapping("/YZUserInfo/manage")
public class YzUserController extends BaseController
{
    private String prefix = "YZUserInfo/manage";

    @Autowired
    private IYzUserService yzUserService;

    @RequiresPermissions("YZUserInfo:manage:view")
    @GetMapping()
    public String manage()
    {
        return prefix + "/manage";
    }

    /**
     * 查询账号管理列表
     */
    @RequiresPermissions("YZUserInfo:manage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YzUser yzUser)
    {
        startPage();
        List<YzUser> list = yzUserService.selectYzUserList(yzUser);
        return getDataTable(list);
    }

    /**
     * 导出账号管理列表
     */
    @RequiresPermissions("YZUserInfo:manage:export")
    @Log(title = "账号管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YzUser yzUser)
    {
        List<YzUser> list = yzUserService.selectYzUserList(yzUser);
        ExcelUtil<YzUser> util = new ExcelUtil<YzUser>(YzUser.class);
        return util.exportExcel(list, "账号管理数据");
    }

    /**
     * 上传账号模版文件，一键创建账号
     */
    @Log(title = "账号管理", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file) throws IOException {
        ExcelUtil<YzUser> util = new ExcelUtil<>(YzUser.class);
        List<YzUser> list = util.importExcel(file.getInputStream());
        return toAjax(yzUserService.upload(list));
    }

    /**
     * 新增账号管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存账号管理
     */
    @RequiresPermissions("YZUserInfo:manage:add")
    @Log(title = "账号管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YzUser yzUser)
    {
        logger.info("user: {}",yzUser);
        return toAjax(yzUserService.insertYzUser(yzUser));
    }

    /**
     * 修改账号管理
     */
    @RequiresPermissions("YZUserInfo:manage:edit")
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        YzUser yzUser = yzUserService.selectYzUserByUserId(userId);
        mmap.put("yzUser", yzUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存账号管理
     */
    @RequiresPermissions("YZUserInfo:manage:edit")
    @Log(title = "账号管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YzUser yzUser)
    {
        return toAjax(yzUserService.updateYzUser(yzUser));
    }

    /**
     * 删除账号管理
     */
    @RequiresPermissions("YZUserInfo:manage:remove")
    @Log(title = "账号管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        return toAjax(yzUserService.deleteYzUserByUserId(Long.valueOf(id)));
    }
}
